package com.itcast.tanhuasso.controller;

import com.itcast.tanhuasso.service.HuanXinService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @PachageName: com.itcast.tanhuasso.controller
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/2213:47
 * @Version 1.0
 */
@RestController
@RequestMapping("user/huanxin")
@Slf4j
public class HuanXinController {

    @Autowired
    private HuanXinService huanXinService;
    @PostMapping(value = "contacts/{owner_username}/{friend_username}")
    public ResponseEntity<Void> contactUsers(@PathVariable("owner_username")Long userId,@PathVariable("owner_username")Long friendId){
        try {
            Boolean bool = this.huanXinService.contactUsers(userId,friendId);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("环信添加好友错误~ userId=" + userId + ",friendId=" + friendId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 发送系统消息
     *
     * @param target
     * @param msg
     * @param type
     * @return
     */
    @PostMapping("messages")
    public ResponseEntity<Void> sendMsg(@RequestParam("target") String target,
                                        @RequestParam("msg") String msg,
                                        @RequestParam(value = "type", defaultValue = "txt") String type){

        try {
            Boolean bool = this.huanXinService.sendMsg(target,msg,type);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("发送系统消息错误~ target=" + target + ",msg=" + msg + ", type= " + type,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
